package com.itheima.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)//只用于处理SQLIntegrityConstraintViolationException这个异常
    public R<String> handlerSqlException(SQLIntegrityConstraintViolationException exception){
        String message = exception.getMessage();
        if (message.contains("Duplicate entry")){
            String username = message.split(" ")[2];
            return R.error(username+"已经存在");
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(CustomException.class)//只用于处理CustomException这个异常
    public R<String> handlerSqlException(CustomException exception){
        String message = exception.getMessage();

        return R.error(message);
    }
}
